import { ActionBase } from "../../ActionBase";

/**
 * 放置site Action
 */
export class ActionTowerRepair  extends ActionBase<IActionCMD<"towerRepair">>
{
    public execute(action: IActionCMD<"towerRepair">): ActionCode 
    {
       
        const room = action.arg.entity as Required<Room>;
        if(!room)
        {
            return 0;
        }
        const towers = room.towers();
         
        const targets = Object.keys(action.arg.task.data.arg.targets);
        let firstObject :Structure|undefined 
        while(towers.length>0 && targets.length>0)
        {
            const top = towers.pop() as StructureTower;
            let id = targets.pop() as Id<Structure>;
            let Gameobj =  Game.getObjectById(id);
            while(!Gameobj)
            {
                id = targets.pop() as Id<Structure>;
                Gameobj =  Game.getObjectById(id);
            }
            if(Gameobj)
            {
                top.repair(Gameobj);
                firstObject = Gameobj;
            }
            
        }
        if(firstObject)
        {
            towers.forEach(e=>e.repair(firstObject!));
        }
        
        return 0;
    }
}